home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v8n18.arc / HEXASC.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-10-02  |  2.9 KB  |  106 lines

  1.  
  2.         title   HEXASC -- convert binary to hex ASCII
  3.         page    55,132
  4.         .386
  5.  
  6. ; HEXASC.ASM:   Convert binary data to hex ASCII.
  7. ; Copyright (C) 1989 Ziff Communications Co.
  8. ; PC Magazine * Ray Duncan
  9. ;
  10. ; This module contains 3 public routines:
  11. ; D2ASC:        Convert binary dword (32-bits) to ASCII string
  12. ; W2ASC:        Convert binary word  (16-bits) to ASCII string
  13. ; B2ASC:        Convert binary byte  (8-bits)  to ASCII string
  14.  
  15. _TEXT   segment dword use32 public 'CODE'
  16.  
  17.         assume  cs:_TEXT
  18.  
  19. ; D2ASC:        Convert doubleword (32-bits) to hex ASCII
  20. ;
  21. ; Call with:    EAX    = data to convert
  22. ;               ES:EDI = storage address for ASCII string
  23. ;
  24. ; Returns:      nothing
  25. ;
  26. ; Uses:         EAX, ECX, EDI
  27. ;
  28.         public  d2asc
  29. d2asc   proc    near
  30.  
  31.         push    eax                     ; save copy of dword
  32.         shr     eax,16                  ; convert upper word
  33.         call    w2asc
  34.         pop     eax                     ; get back copy of dword
  35.         call    w2asc                   ; convert lower word
  36.         ret
  37.  
  38. d2asc   endp
  39.  
  40. ; W2ASC:        Convert word (16-bits) to hex ASCII
  41. ;
  42. ; Call with:    EAX    = data to convert in lower 16-bits
  43. ;               ES:EDI = storage address for ASCII string
  44. ;
  45. ; Returns:      nothing
  46. ;
  47. ; Uses:         EAX, ECX, EDI
  48. ;
  49.         public  w2asc
  50. w2asc   proc    near
  51.  
  52.         push    eax                     ; save copy of value
  53.         mov     al,ah
  54.         call    b2asc                   ; convert upper byte
  55.  
  56.         pop     eax                     ; get back copy
  57.         call    b2asc                   ; convert lower byte
  58.         ret
  59.  
  60. w2asc   endp
  61.  
  62. ; B2ASC:        Convert byte (8-bits) to hex ASCII
  63. ;
  64. ; Call with:    EAX    = data to convert in lower 8-bits
  65. ;               ES:EDI = storage address for ASCII string
  66. ;
  67. ; Returns:      nothing
  68. ;
  69. ; Uses:         EAX, ECX, EDI
  70. ;
  71.         public  b2asc
  72. b2asc   proc    near
  73.  
  74.         ror     eax,4
  75.         call    ascii                   ; convert high 4 bits
  76.         stosb                           ; and store ASCII character
  77.         rol     eax,4
  78.         call    ascii                   ; convert low 4 bits
  79.         stosb                           ; and store ASCII character
  80.         ret
  81.  
  82. b2asc   endp
  83.  
  84. ; ASCII:        Convert nibble (4-bits) to hex ASCII
  85. ;
  86. ; Call with:    AL    = data to convert in lower 4-bits
  87. ; Returns:      AL    = ASCII character
  88. ; Uses:         nothing
  89. ;
  90. ascii   proc    near
  91.                 
  92.         and     al,0fh                  ; mask to range 00H-0FH
  93.         add     al,'0'                  ; offset to chars '0'-'9'
  94.         cmp     al,'9'                  ; is result > '9'?
  95.         jle     ascii2                  ; no, jump
  96.         add     al,'A'-'9'-1            ; adjust for chars 'A'-'F',
  97.  
  98. ascii2: ret                             ; return AL = ASCII char.
  99.  
  100. ascii   endp
  101.  
  102. _TEXT   ends
  103.  
  104.         end
  105.  
  106.